//公共
import myHeader from "/pages/headerfoot/pages/header.vue";
//骨架
import uniSkeleton from "/plugin/skeleton/components/heaven-skeleton/heaven-skeleton.vue";
//公共公益方法
import commonBenefit from '@/common/commonbenefit.js';
//公共方法
import Base from '@/common/base.js';
//语言
import Lang from '@/utils/lang.js';
//微信api
import weChat from '@/lib/wechat.js';
//cache缓存
import Cache from '@/utils/cache.js';


export default {
	components: {
		myHeader,
		uniSkeleton,
	},
	data() {
		return {
			seconds: 1, //秒
			projectname: '', //名称
			benefittitle: '', //头部名称
			articlecontent: '', //内容
			loading: true,
			projectid: 0,
			langs: Lang.lang,
			finishreading: Lang.lang.finishreading, //阅读完毕
			timer: {}, //计时器
			post_str_length: 0, // 字体长度
			constraintlength:0,//限制长度
		}
	},

	onShow() {
		var that = this;
		that.ifreglogin();
	},
	onLoad(option) {
		var that = this;
		//得到项目信息
		var data = {};
		data['project_id'] = option.projectid;
		data['user_id'] =Cache.get('userid');
		that.projectid = option.projectid; //科目
		commonBenefit.getProjectApiMethod(data).then(res => {
			if (res) {
				that.loading = false;
				that.time = res.minute; //分钟
				that.second = res.second; //秒
				that.projectname = res.project_article.article_title; //名称
				that.benefittitle = res.project_name; //头部名称
				that.articlecontent = res.project_article.article_content; //内容
				that.post_str_length = res.project_article.post_str_length; //字体长度
				that.constraintlength = res.reading_speed; //限制长度
				that.countdown(); //倒计时
			}
		});
	},
	methods: {
		//判断是否需要注册或者登录与其他
		async ifreglogin() {
			var that = this;
			//注册
			await Base.globalauthorization();

			//判断学生是否注册
			await commonBenefit.ifStudenter().then(res => {
				if (res) {
					uni.redirectTo({
						url: commonBenefit.automaticurl('studentreg')
					})
				}
			});
		},
		//正记时
		countdown() {
			var that = this;
			that.timer = setInterval(() => {
				that.seconds = ++that.seconds; //计时器
			}, 1000);
		},
		//提交
		submit() {
			var that = this;
			var iferror = 0;
			//判断是否挂机
			var nownumber = (that.post_str_length / that.seconds) * 60;
				if (nownumber>that.constraintlength) {
					iferror = 1; //有错误
				}
		 
			if (iferror == 1) {
				uni.showToast({
					title: '严禁刷题，请认真阅读',
					icon: 'none',
					duration: 2000
				})
			} else {
				//清除定时器
				clearInterval(that.timer);
				var url = commonBenefit.automaticurl('projectanswer') + "?projectid=" + that.projectid +"&seconds=" + that.seconds;
				uni.redirectTo({
					url: url,
				})
			}

		}

	}
}